﻿using HandyControl.Controls;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using WpfAGVSystem.Common;
using WpfAGVSystem.DBModel;
using WpfAGVSystem.InterFace;

namespace WpfAGVSystem.ViewModels
{
    public class StationNetFormViewModel : BindableBase, INavigationAware
    {
        private IDBInterface dB;
        public StationNet_Entity Selset { get; set; }
        public DelegateCommand<string> OperationCommand { get; private set; }
        public DelegateCommand DisplayCommand { get; private set; }
        public ObservableCollection<StationNet_Entity> DataList { get; set; } = new ObservableCollection<StationNet_Entity>();
        public ObservableCollection<StationNet_Entity> DataListDisplay { get; set; } = new ObservableCollection<StationNet_Entity>();
        public int Count;
        public int Index { get; set; }
        public int PangCount { get; set; } = 15;
        private bool Flag { get; set; }
        public int Pages { get; set; } = 0;
        private bool caninit(string name)
        {
            return Flag;
        }
        public StationNetFormViewModel(IDBInterface dBInterface)
        {

            Flag = true;
            OperationCommand = new DelegateCommand<string>(Operation, caninit).ObservesProperty(() => Flag);
            dB = dBInterface;
            DisplayCommand = new DelegateCommand(() => {
                DataListDisplay = new ObservableCollection<StationNet_Entity>(DataList.Skip((Index - 1) * PangCount).Take(PangCount));
            });
        }
        private void init()
        {
            Index = 1;
            DataList.Clear();
            var list = dB.Where<StationNet_Entity>().ToList();
            Count = list.Count();
            Pages = Count / PangCount;
            foreach (StationNet_Entity entity in list)
            {
                DataList.Add(entity);
              
            }
           
            DataListDisplay = new ObservableCollection<StationNet_Entity>(DataList.Skip((Index - 1) * PangCount).Take(PangCount));
        }
        private void Operation(string str)
        {
            Flag = false;
            switch (str)
            {
                case "add":
                    DataList.Add(new StationNet_Entity());
                    Selset = null;
                    break;
                case "clone":
                    if (Selset != null)
                    {
                        DataList.Add(Selset.Clone());
                        Selset = null;
                    }
                    else
                    {
                        Growl.InfoGlobal("请先选中需要复制的行");
                    }
                    break;
                case "resh":
                    init();
                    break;
                case "delete":
                    if (Selset != null)
                    {
                        if (dB.Delete<StationNet_Entity>(x => x.id == Selset.id))
                        {
                            Growl.InfoGlobal("删除成功");
                        }
                        else
                        {
                            Growl.InfoGlobal("删除失败");
                        }
                    }
                    init();
                    break;
                case "updata":
                    if (Selset != null)
                    {
                        if (Selset.id == 0)
                        {
                            if (dB.Add(Selset))
                            {
                                Growl.InfoGlobal("添加成功");
                            }
                            else
                            {
                                Growl.InfoGlobal("添加失败");
                            }
                        }
                        else
                        {
                            if (dB.Update(Selset))
                            {
                                Growl.InfoGlobal("更新成功");
                            }
                            else
                            {
                                Growl.InfoGlobal("更新失败");
                                init();
                            }
                        }

                    }
                    else
                    {
                        Growl.InfoGlobal("请先选中该行");
                    }
                    break;
                default:


                    break;
            }
            Flag = true;
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            init();
            //  throw new NotImplementedException();
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
            // throw new NotImplementedException();
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            // throw new NotImplementedException();
        }
    }
}
